package com.ithiema.reggie.common;

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

//mybtis (买呗忒四)  下的自动填充功能  {要想使用还需在实体类上写@TableField()}
//实现MetaObjectHandler接口
@Component
@Slf4j
public class MyMetaObjecthandler implements MetaObjectHandler {

    //插入操作，自动填充   (新增)
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("createUser", BaseContext.getCurrentId());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
    //更新操作，自动填充   (修改)
    @Override//
    public void updateFill(MetaObject metaObject) {
        //更新时没有创建人和创建时间
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateUser", BaseContext.getCurrentId());
    }
}
